#'
#' @title 表达谱格式转换
#' @description 表达谱格式转换
#' @details 来自https://www.jieandze1314.com/post/cnposts/86/ https://haroldpimentel.wordpress.com/2014/05/08/what-the-fpkm-a-review-rna-seq-expression-units/
#' @param method 字符串，count_to_tpm，count_to_Fpkm，fpkm_to_tpm
#' @param expr 表达谱矩阵 
#' @param effLen 表达谱中，对应的基因的长度
#' @export
#' @return 表达谱矩阵
#' @examples
#' @author *WYK*
#'
ExprConversion <- function(method, expr, effLen) {

    data <- as.matrix(expr)
    f <- switch(method,
        count_to_tpm = function(counts = data, effLen) {
            rate <- log(counts) - log(effLen)
            denom <- log(sum(exp(rate)))
            exp(rate - denom + log(1e6))
        },
        count_to_fpkm = function(counts = data, effLen) {
            N <- sum(counts)
            exp(log(counts) + log(1e9) - log(effLen) - log(N))
        },
        fpkm_to_tpm = function(fpkm = data,...) {
            exp(log(fpkm) - log(sum(fpkm)) + log(1e6))
        }
    )

    res <- apply(data,2,FUN = f,effLen = effLen)
    return(res)
}
# countToTpm <- function(counts, effLen)
# {
#     rate <- log(counts) - log(effLen)
#     denom <- log(sum(exp(rate)))
#     exp(rate - denom + log(1e6))
# }
 
# countToFpkm <- function(counts, effLen)
# {
#     N <- sum(counts)
#     exp( log(counts) + log(1e9) - log(effLen) - log(N) )
# }
 
# fpkmToTpm <- function(fpkm)
# {
#     exp(log(fpkm) - log(sum(fpkm)) + log(1e6))
# }
 
# countToEffCounts <- function(counts, len, effLen)
# {
#     counts * (len / effLen)
# }
 